package jt.classic.system.workflow.impl;

import jt.classic.system.user.IUser;
import jt.classic.system.workflow.WActivity;
import jt.classic.system.workflow.WExecutor;
import jt.classic.system.workflow.WFlow;
import jt.classic.system.workflow.WInstance;
import jt.classic.system.workflow.WStep;

import org.limp.basework.AbstractSimpleBean;
import org.limp.basework.Table;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 流程活动记录接口实现类
 *
 * @author bizkit
 *
 */
public class WActivityImpl extends AbstractSimpleBean implements WActivity {
    private String id;
    private String lastid;
    private String title;
    private String starttime;
    private String endtime;
    private String lmtime;
    private String process;
    private String group;
    private WInstance ins;
    private String stepid;
    private String backmemo;
    private boolean lock;
    private IUser locker;
    private int state;
    private int type;
    private WExecutor executor;
    private List nextlist;
    private Map contents;
    private List backlist;
    
    public String id() {
        // TODO Auto-generated method stub
        return this.id;
    }

    public String iendtime() {
        // TODO Auto-generated method stub
        return this.endtime;
    }

    public String igroup() {
        // TODO Auto-generated method stub
        return this.group;
    }

    public WInstance instance() {
        // TODO Auto-generated method stub
        return this.ins;
    }

    public WActivity ilast() {
        // TODO Auto-generated method stub
        WActivity act = null;

        if (this.lastid.equals("0")) {
            act = this.ins.iactivity(this.id);
        } else {
            act = this.ins.iactivity(lastid);
        }

        return act;
    }

    public List inext() {
        if (this.nextlist == null) {
            List list = this.ins.iactivitylist();

            if ((list != null) && !list.isEmpty()) {
                this.nextlist = new ArrayList();

                for (int i = 0; i < list.size(); i++) {
                    WActivity act = (WActivity) list.get(i);
                    String lastid = (String) act.getResource().get("A_LASTID");

                    if (this.id.equals(lastid)) {
                        this.nextlist.add(act);
                    }
                }
            }
        }

        return this.nextlist;
    }

    public String ilmtime() {
        // TODO Auto-generated method stub
        return this.lmtime;
    }

    public boolean ilock() {
        // TODO Auto-generated method stub
        return this.lock;
    }

    public IUser ilocker() {
        // TODO Auto-generated method stub
        return this.locker;
    }

    public String iprocess() {
        // TODO Auto-generated method stub
        return this.process;
    }

    public String istarttime() {
        // TODO Auto-generated method stub
        return this.starttime;
    }

    public int istate() {
        // TODO Auto-generated method stub
        return this.state;
    }

    public int itype() {
        return this.type;
    }

    public WStep istep() {
        // TODO Auto-generated method stub
        WFlow flow = this.ins.iflow();

        return (flow == null) ? null : flow.istep(this.stepid);
    }

    public String ititle() {
        // TODO Auto-generated method stub
        return this.title;
    }

    public String ibackmemo() {
        return this.backmemo;
    }

    public List icontent(Table table) {
        return ((this.contents != null) &&
        this.contents.containsKey(table.getName()))
        ? (List) this.contents.get(table.getName()) : null;
    }

    public void reloadContent(Map content) {
        if (content != null) {
            this.contents = content;
        }
    }

    public WExecutor iexecutor() {
        return this.executor;
    }

    public boolean canretake() {
        if ((this.state == -2) || (this.state == -3)) {
            return false;
        } else if (this.state == 0) {
            return !this.lock;
        }

        return false;
    }

    public List ilist4back() {
        if (this.backlist == null) {
            this.backlist = new ArrayList();

            List list = this.instance().iactivitylist();

            if ((list != null) && !list.isEmpty()) {
                Map map = new HashMap();

                for (int i = 0; i < list.size(); i++) {
                    WActivity act = (WActivity) list.get(i);

                    if ((Integer.parseInt(act.id()) < Integer.parseInt(this.id)) &&
                            (act.istep().inner() == this.istep().inner()) &&
                            (act.istate() == 2)) {
                        /*经历过相同步骤*/
                        if (act.istep().equals(this.istep())) {
                            /*不同人处理*/
                            if (!act.iexecutor().iuser().id().equals(this.iexecutor()
                                                                             .iuser()
                                                                             .id())) {
                                this.backlist.add(act);
                            }
                        } else {
                            if (!map.containsKey(act.istep().id())) {
                                map.put(act.istep().id(), act.istep().id());

                                this.backlist.add(act);
                            }
                        }
                    }
                }
            }

            /*if ((this.lastid != null) && !this.lastid.equals("0")) {
                WActivity act = this.ins.iactivity(this.lastid);

                if (!this.backlist.contains(act)) {
                    this.backlist.add(act);
                }
            }*/
        }

        return this.backlist;
    }

    public boolean iowner(IUser user) {
        return (user != null) && this.executor.iuser().id().equals(user.id());
    }

    public void reload(Map info) {
        super.reload(info);
        this.id = (String) info.get("A_ID");
        this.ins = (WInstance) info.get("INSTANCE");
        this.title = (String) info.get("A_TITLE");
        this.starttime = (String) info.get("A_STIME");
        this.endtime = (String) info.get("A_ETIME");
        this.lmtime = (String) info.get("A_LTIME");
        this.lock = "1".equals(info.get("A_LOCK"));
        this.locker = (IUser) info.get("LOCKER");
        this.process = (String) info.get("A_PROCESS");
        this.group = (String) info.get("A_GROUP");
        this.state = Integer.parseInt((String) info.get("A_STATE"));
        this.type = Integer.parseInt((String) info.get("A_TYPE"));
        this.stepid = (String) info.get("S_ID");
        this.lastid = (String) info.get("A_LASTID");
        this.executor = (WExecutor) info.get("EXECUTOR");
        this.contents = (Map) info.get("CONTENTS");
        this.backmemo = (String) info.get("BACK_MEMO");
    }
}
